package com.shop.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.shop.config.MessageConfig;
import com.shop.entity.Message;
import com.shop.entity.Order;
import com.shop.mapper.MessageMapper;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;

/**
 * (Message)表服务接口
 *
 * @author makejava
 * @since 2020-11-21 12:11:06
 */

@Service
public class MessageService {
    @Resource
    private  MessageMapper messageMapper;

    @Autowired
    RabbitTemplate rabbitTemplate;

    public void updateOrderStatus(String message) {

       // Object parse = JSO

//        JSONObject jsonObject = JSONObject.parseObject(message);
//
//        String id = jsonObject.getString("id");
//        Integer intId = (Integer) JSON.parse(id);
        //查询订单状态
        Order order = messageMapper.selectOrderStatusById(Integer.parseInt(message));

        if(!StringUtils.isEmpty(order))
        {
            if(order.getStatus().equals("0"))
            {
                messageMapper.updateOrderStatus(order.getId());
            }
        }

        rabbitTemplate.convertAndSend(MessageConfig.Merchant_Exchange,"merchant"+order.getMerchantId(),"用户未支付，订单自动取消");

    }

}